//模块基类
Ext.BLANK_IMAGE_URL = "./js/ext/resources/images/default/s.gif";
Ext.namespace('demo');
demo.module = function(tab) {
	this.main = tab;
	this.init();		//初始化方法
}

Ext.extend(demo.module, Ext.util.Observable, {
			init : Ext.emptyFn
		});

//主程序类
demo.app = function() {
	this.init();
}
Ext.extend(demo.app, Ext.util.Observable, {
	init : function() {

		this.left = new Ext.tree.TreePanel({
			title:"新闻发布系统后台管理",
			region : 'west',
			margins : '0 0 0 5',
			width : 250,
			split : true,
			minSize : 200,
			maxSize : 300,
			autoScroll : true,
			bodyStyle : 'padding:10px',
			collapsible:true,
			loader : new Ext.tree.TreeLoader({
				url : 'http://localhost:8080/itnetwork/js/master/data/tree.txt',
				requestMethod : 'GET'
			}),
			//构造根节点
			root : new Ext.tree.AsyncTreeNode({
						id : 'root',
						text : '功能菜单',
						expanded : true
					})
		});
		
		this.left.on('click', this.clickTree, this);

		var tab =  new Ext.Panel({
		    title: '说明',   
		    id: 'docs',
		    bodyStyle:'padding:10px',
		    autoLoad: 'docs.html', 
		    html:"<h1>说明文档</h1>",
		    autoload:false,
		    layout: 'fit' 
		}); 

		this.center = new Ext.TabPanel({
			title:"操作面板",
			region : 'center',
			margins : '0 5 0 0',
			activeTab: 0,
			autoScroll : true,
			items: [tab]
			});
			
		this.center.on('tabchange', this.changeTab, this);

		this.top = new Ext.Panel({
					region : "north",
					title : "LOGO",
					height : 80,
					html : "这里放LOGO"
				});
		this.right = new Ext.Panel({
					region : "east",
					title : "未定义",
					collapsible : true,
					split : true,
					containerScroll : true,
					autoScroll : true,
					width : 200
				});
		this.bottom = new Ext.Panel({
					region : "south",
					html : "版权所有，翻版必究",
					bodyStyle : "padding: 10px;text-align: center; font-size:12px"
				});
		var viewport = new Ext.Viewport({
					layout : 'border',
					// collapsible:true,
					items : [this.right,this.bottom,this.center,this.top,this.left]
				});
				
		this.loadMask = new Ext.LoadMask(this.center.body);
	},

	//切换TabPanel标签方法
	changeTab : function(p, t) {
		//如果存在相应树节点，就选中,否则就清空选择状态
		var id = t.id.replace('tab-', '');
		var node = this.left.getNodeById(id);
		if (node) {
			this.left.getSelectionModel().select(node);
		} else {
			this.left.getSelectionModel().clearSelections();
		}
	},

	//点击菜单方法(node:被点击的节点)
	clickTree : function(node) {			//事件调用，this由注册时指定
		//如果节点不是叶子则返回
		if (!node.isLeaf()) {
			alert("false");
			return false;
		}

		var id = 'tab-' + node.id;
		var tab = Ext.getCmp(id);
		if (!tab) {
			tab = this.center.add(new Ext.Panel({	//这里new了Panel类型选项tab
						id : id,
						title : node.text,
						layout : 'fit',
						closable : true
					}));
			this.center.setActiveTab(tab);
			//加载模块
			alert(node.id);
			this.loadModel(node.id, tab);
		} else {
			this.center.setActiveTab(tab);
		}
	},

	//加载模块方法(id:模块ID;tab:模块显示在哪里)
	loadModel : function(id, tab) {
		//定义模块变量
		var model;

		if (this[id]) {
			//如果模块类已存在，就直接实例化
			model = new this[id](tab);
		} else {
			this.loadMask.show();
			Ext.Ajax.request({
				method : 'GET',
				url : 'http://localhost:8080/itnetwork/js/master/modules/'
						+ id + '.js',
				scope : this,
				success : function(response) {
					//获取模块类  
					this[id] = eval(response.responseText);
					//实例化模块类
					alert(this[id]);
					model = new this[id](tab);
					alert(model);
					this.loadMask.hide();
				}
			});
		}
	}
});

//实例化主程序类
//Ext.BLANK_IMAGE_URL = 'http://localhost:8080/Extjs/script/ext/resources/images/default/s.gif';
Ext.form.Field.prototype.msgTarget = 'title';
Ext.onReady(function() {
			Ext.QuickTips.init();
			myApp = new demo.app();
		});